//index.js
//获取应用实例
const app = getApp()
Page({
     data: {
          indicatorDots: true,
          autoplay: true,
          interval: 5000,
          duration: 1000,
          recommend:[],//列表
          pages:1,//页数
     },
     onLoad: function() {
          var that=this
          var pages=that.data.pages
          that.setData({
               userid: wx.getStorageSync("userid")
          })
          wx.showLoading({
               title: '加载中...',
          })
          // 获取权限
          wx.request({
               url: app.data.apiurl +'/ddk/index',
               data: {
                    page: pages,
                    pageSize:20  
               },
               header: {
                    'content-type': 'application/json' // 默认值
               },
               method: 'GET',
               dataType: 'json',
               success: function(res) {
                    wx.hideLoading()
                    // 过滤banner多余代码
                    var vbanner = res.data.data.banner
                    vbanner.forEach(function (item, index, array) {
                         array[index] = {
                              id: item.goods_id,
                              img: item.goods_image_url,
                         }
                    });
                    //  过滤recommend多余代

                    var vrecommend = res.data.data.recommend
         
                    vrecommend.forEach(function (item, index, array) {
                         if (((item.min_normal_price / 100).toString()).indexOf(".") == "-1") {
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group: (item.min_group_price - item.coupon_discount) / 100,
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         } else {
                              var nun = (item.min_normal_price / 100).toString().split(".")[1].length
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group: ((item.min_group_price - item.coupon_discount) / 100).toFixed(nun),
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         } 
                    });
                    that.setData({
                         banner: vbanner,
                         fenlei: res.data.data.opt,
                         theme: res.data.data.theme,
                         recommend: vrecommend
                    })
               },
               fail: function(res) {
                    wx.showToast({
                         title: '网络异常',
                         icon: 'none',
                         duration: 2000
                    })
               }
          })
          
     },
     searchbut: function() {
          wx.navigateTo({
               url: '/pages/search/search',
          })
     },
     imginfo:function(e){
          this.setData({
               imgheight: e.detail.height
          })
     },
     onReachBottom: function (res) {
          var that = this
          var pages = that.data.pages
          pages++
          that.setData({
               pages: pages
          })
          wx.showLoading({
               title: '加载中...',
          })
          wx.request({
               url: app.data.apiurl +'/ddk/index/recommend',
               data: {
                    page: pages,
                    pageSize: 10
               },
               header: {
                    'content-type': 'application/json' // 默认值
               },
               method: 'GET',
               dataType: 'json',
               success: function (res) {
                    wx.hideLoading()
                    //  过滤recommend多余代码
                    var rec = that.data.recommend
                    var vrecommend = res.data.data.recommend
                    vrecommend.forEach(function (item, index, array) {
                         if (((item.min_normal_price / 100).toString()).indexOf(".") == "-1") {
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group:(item.min_group_price - item.coupon_discount) / 100,
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         } else {
                              var nun = (item.min_normal_price / 100).toString().split(".")[1].length
                              array[index] = {
                                   img: item.goods_thumbnail_url,
                                   name: item.goods_name,
                                   id: item.goods_id,
                                   group: ((item.min_group_price - item.coupon_discount) / 100).toFixed(nun),
                                   normal: item.min_group_price / 100,
                                   discount: item.coupon_discount / 100,
                                   quantity: item.sold_quantity
                              }
                         }
                    });
                    for (var i = 0; i < vrecommend.length; i++) {
                         rec.push(vrecommend[i]);
                    }
                    that.setData({
                         recommend: rec
                    })
               },
               fail: function (res) {
                    wx.showToast({
                         title: '网络异常',
                         icon: 'none',
                         duration: 2000
                    })
               }
          })
     },
     onShareAppMessage: function (res) {
          var that = this
          if (res.from === 'button') {
               // 来自页面内转发按钮
               //console.log(res.target)
          }
          return {
               title: "拼多多优惠券",
               path: '/pages/index/index',
               success: function (res) {
                    //console.log(res)
                    wx.showToast({
                         title: '发送成功',
                         icon: 'none'
                    })
               },
               fail: function (res) {
                    // 转发失败
                    wx.showToast({
                         title: '发送失败',
                         icon: 'none'
                    })
               }
          }

     }
})